<template>
  <div class="sidebar">
    <div class="avater">
        <img src="../assets/img/avater.jpg" alt="">
    </div>
    <div class="sideBox">
        <div v-for="(item, index) in list" @click="toRouter(item,index)" :key="index"
        :class="path == item.path ? 'active': ''"
        class="sidebarItem">
        <i class="iconfont" :class="item.icon"></i>
        <span>{{ item.title }}</span>
        </div>
    </div>
  </div>
</template>

<script>
import { toRefs, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
export default {
    setup() {
        const state = reactive({
            list: [
                {
                    title: '首页',
                    path: '/',
                    icon: 'icon-B'
                },
                {
                    title: '类型列表',
                    path: '/articleType'  ,
                    icon: 'icon-shouyejiedan'  
                },
                {
                    title: '文章列表',
                    path: '/articleList',
                    icon: 'icon-shouyejiedan'
                },
                {
                    title: '留言板',
                    path: '/comment',
                    icon: 'icon-shouyejiedan'
                },
                {
                    title: '工具库',
                    path: '/tool',
                    icon: 'icon-shouyejiedan'
                },
                {
                    title: '设置',
                    path: '/setting',
                    icon: 'icon-yunliankeji_gongyinglianfuben'
                },
                {
                    title: '退出',
                    path: '/leave',
                    icon: 'icon-tuichu'
                }
            ],
            path: '/'
        })
        const router = useRouter()
        const route = useRoute()
        onMounted(() => {
            state.path = route.path
        })
        const toRouter = (item,index) => {
            if(index == state.list.length -1){
                //退出
                ElMessageBox.confirm('确定退出登陆吗, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    localStorage.removeItem('token')
                    router.replace({ path: '/login'})
                    ElMessage({
                    type: 'success',
                    message: '退出登陆成功!'
                    });
                }).catch(() => {
                    
                });
                return
            }
            if(route.path == item.path) return console.log('同一路由不进行跳转')
            state.path = item.path
            router.push(item.path)
        }

        return {
            ...toRefs(state),
            toRouter,
        }
    }
}
</script>

<style lang="scss" scoped>
.sidebar{
    width: 250px;
    height: 100%;
    background-color: #FFAFBD;
    transition: all 0.3s;
    border-radius: 20px 0 0 20px;
    .avater{
        display: flex;
        justify-content: center;
        align-items: center;
        height: 120px;
        img{
            width: 80px;
            height: 80px;
            border-radius: 50%;
        }
    }
    .sideBox{
        padding-left: 50px;
        .active{
                border-radius: 40px 0 0 40px;
                background-color: #fff;
                color: #FFAFBD!important;
                cursor: pointer;
        }
        .sidebarItem{
            height: 40px;
            line-height: 40px;
            color: #ffffff;
            padding-left: 20px;
            margin: 0 0 20px 0;
            display: flex;
            align-items: center;
            i{
                margin-right: 5px;
            }
            &:hover{
                border-radius: 40px 0 0 40px;
                background-color: #fff;
                color: #FFAFBD;
                cursor: pointer;
            }
        }
    }
}
@media screen and (max-width: 600px) {
    .sidebar{
        width: 100px;
        .avater{
            img{
                width: 40px;
                height: 40px;
            }
        }
        .sideBox{
            padding-left: 10px;
            font-size: 10px;
            .sidebarItem{
                justify-content: center;
                    span{
                        display: none;
                    }
                }
        }
    }
}
</style>